<?php 
	namespace Account\Controller;
	use Think\Model;
	class ShopController extends BaseController{
		function index(){
			if(!S('shopping')){
				$sp = M('Shopping')->select();
				S('shopping',$sp);
			}
			$array['addshop'] = S('shopping');
			$array['num'] = 2;
			$array['user_account'] = session('user_account');
			$this->assign($array);
			$this->display('home2');
		}
		function shop(){
			if(IS_GET){
				if($shop = M('shopping')->where(I('get.'))->find()){
					$this->assign('shop',$shop);
				}
			}
			$this->display('introduction');
		}
		function register(){
			if(IS_POST){
				$account = $account = D('UserAccount');
				if($account->create('',4)){
					if($account->add())
					$this->success("注册成功...!",'loginout');
				}else{
					$this->error($account->getError());
				}
			}
			$this->display();
		}
		function login(){
			if(IS_POST){
				$account = D('UserAccount');
				if($a = $account->create('',5)){
					if($user = $account->where($a)->field('id,username,charge')->find()){
						$car = D('ShopCar')->where("user_id = {$user['id']}")->count('id');
						session('user_account',$user);	
						$this->redirect('index');
					}else{
						$this->error('您的用户名和密码不匹配...!');
					}
				}else{
					$this->error($account->getError());
				}
			}	
			$this->display();
		}
		function charge(){
			if(IS_POST){
				$account = D('UserAccount');
				$arr = array(
					'charge' => I('post.charge')+session('user_account.charge')
				);
				if($account->where('id='.session('user_account.id'))->save($arr)){
					if($a = $account->where('id='.session('user_account.id'))->find()){
						session('user_account',$a);
						$this->ajaxReturn($a);
					}
				}
			}
		}
		function shopcart(){
			if(!session('?user_account')){
				$this->error('您没有登陆，请您登陆...!');
			}
			$shopcar = M('ShopCar');
			$sc = $shopcar->join('shopping on shop_car.shop_id = shopping.id')->field('shop_car.id car_id,shop_id,count,name,imgpath,desecription,price,price*count num')->where('user_id='.session('user_account.id'))->select();
			$this->assign('shopcar_shop',$sc);
			$this->display();
		}
		function addshopcat(){
			if(session('?user_account')){
				if($_POST['type'] == 'addsc'){
					$shopcar = D('ShopCar');
					$shop = $shopcar->create();
					$shop['user_id'] = session('user_account.id');
					$userid['user_id'] = session('user_account.id');
					if($shopcar->add($shop) && $count = $shopcar->where($userid)->count('id')){
						session('shopcar_count',$count);
						$this->ajaxReturn($count);
					}
				}
			}else{
				$this->ajaxReturn('nologin');
			}
		}
		function loginout(){
			session(null);
			$this->redirect('login');
		}
		function search(){
			$this->display();
		}
		function pay(){
			if(!session('?user_account')){
				$this->error('您没有登陆，请您登陆...!');
			}
			
			$this->display();
		}
		function success(){
			if(!session('?user_account')){
				$this->error('您没有登陆，请您登陆...!');
			}
			$this->display();
		}
		function dir(){
			static $i=1200;
			$a = opendir('F:\php\WWW\git\waihuibao\Public/images');
			while($c = readdir($a)){
				if($c!='.'&&$c!='..'){
					$arr['imgpath'] = '/waihuibao/Public/images/'.$c;
					$arr['name'] = '苹果手机'.$i++;
					$arr['price'] = 5600;
					$arr['desecription'] = $i.'极致画面与世界较真';
					M('Shopping')->data($arr)->add();
				}
			}
			echo 'OK';
			closedir($c);
		}
	}
?>